package avicit.bdp.dds.api.controller;

import avicit.bdp.dds.api.service.TaskNodesService;
import avicit.bdp.dds.dao.entity.ProjectType;
import avicit.bdp.dds.dao.entity.TaskNodes;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 通用算法库管理REST接口
 */
@RestController
@RequestMapping("/api/bdp/dds/treeNode")
@Api(tags = "PHM通用算法库管理Rest接口定义")
public class TaskNodesRest {
    @Autowired
    private TaskNodesService taskNodesService;

    /**
     * 查询分类树
     * @param algoType 0:内置算法、1:自定义算法、2:其他
     * @param showed 0：不显示，1：显示
     * @return
     */
    @ApiOperation(value = "查询分类树")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "算法分类", name = "algoType", dataType = "Int"),
            @ApiImplicitParam(value = "是否显示", name = "showed", dataType = "Int")
    })
    @GetMapping("/getTreeNode")
    public ResponseMsg<List<TaskNodes>> getTreeNode(@RequestParam Integer algoType,
                                                    @RequestParam(required = false) Integer showed) {
        ResponseMsg<List<TaskNodes>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getTreeNode(algoType, showed == null ? 1 : showed));

        return responseMsg;
    }

    /**
     * 查询指定分类下算法节点列表，支持分页查询
     */
    @ApiOperation(value = "查询指定分类下算法节点列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类节点ID", name = "id", dataType = "String"),
            @ApiImplicitParam(value = "算法名称", name = "name", dataType = "String"),
            @ApiImplicitParam(value = "备注", name = "remark", dataType = "String"),
            @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int"),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int")
    })
    @GetMapping("/getAlgoNodeList")
    public ResponseMsg<QueryRespBean<TaskNodes>> getAlgoNodeList(@RequestParam String id,
                                                                 @RequestParam(required = false) String name,
                                                                 @RequestParam(required = false) String remark,
                                                                 @RequestParam(required = false) Integer pageNo,
                                                                 @RequestParam(required = false) Integer pageSize,
                                                                 @RequestParam(required = false) String secretLevel) {
        ResponseMsg<QueryRespBean<TaskNodes>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getAlgoNodeList(id, name, remark,
                pageNo == null ? 1 : pageNo, pageSize == null ? 10 : pageSize, secretLevel));

        return responseMsg;
    }

    /**
     * 查看算法节点详情
     */
    @ApiOperation(value = "查看算法节点详情")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @GetMapping("/getAlgoNodeInfo")
    public ResponseMsg<TaskNodes> getAlgoNodeInfo(@RequestParam String id) {
        ResponseMsg<TaskNodes> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getAlgoNodeInfo(id));

        return responseMsg;
    }

    /**
     * 查看算法说明
     * 如果算法有关联知识，优先显示关联知识，否则显示算法说明
     */
    @ApiOperation(value = "查看算法说明")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @GetMapping("/getAlgorithmRemark")
    public ResponseMsg<TaskNodes> getAlgorithmRemark(@RequestParam String id) {
        ResponseMsg<TaskNodes> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getAlgoNodeInfo(id));

        return responseMsg;
    }

    /**
     * 新增分类节点
     */
    @ApiOperation(value = "新增分类节点")
    @PostMapping("/insertTypeNode")
    public ResponseMsg<Boolean> insertTypeNode(@RequestBody TaskNodes taskNodes) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.insertTypeNode(taskNodes));

        return responseMsg;
    }

    /**
     * 更新分类节点
     */
    @ApiOperation(value = "更新分类节点")
    @PostMapping("/updateTypeNode")
    public ResponseMsg<Boolean> updateTypeNode(@RequestBody TaskNodes taskNodes) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.updateTypeNode(taskNodes));

        return responseMsg;
    }

    /**
     * 上传算法文件
     * @param file Excel模板文件
     * @return 返回remark、settingJson信息
     */
    @ApiOperation(value = "上传算法文件")
    @ApiImplicitParams({@ApiImplicitParam(value = "file", name = "file", dataType = "MultipartFile"),
    })
    @PostMapping("/upload")
    public ResponseMsg<TaskNodes> uploadAlgoFile(MultipartFile file) {
        ResponseMsg<TaskNodes> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.uploadAlgoFile(file));

        return responseMsg;
    }

    /**
     * 新增分类节点
     */
    @ApiOperation(value = "新增算法节点")
    @PostMapping("/insertAlgoNode")
    public ResponseMsg<Boolean> insertAlgoNode(@RequestBody TaskNodes taskNodes) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.insertAlgoNode(taskNodes));

        return responseMsg;
    }

    /**
     * 更新分类节点
     */
    @ApiOperation(value = "更新算法节点")
    @PostMapping("/updateAlgoNode")
    public ResponseMsg<Boolean> updateAlgoNode(@RequestBody TaskNodes taskNodes) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.updateAlgoNode(taskNodes));

        return responseMsg;
    }

    /**
     * 删除分类节点/算法节点
     * @param id 节点ID，多个节点使用英文逗号隔开
     */
    @ApiOperation(value = "删除节点")
    @ApiImplicitParam(value = "id", name = "id", dataType = "String")
    @DeleteMapping("/delete")
    public ResponseMsg<Boolean> delete(@RequestParam String id) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.deleteNode(id));

        return responseMsg;
    }

    /**
     * 查询指定分类下算法节点列表，支持分页查询
     * @param id
     * @return
     * @throws
     */
    @ApiOperation(value = "查询指定分类下算法节点列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "分类节点ID", name = "id", dataType = "String")
    })
    @GetMapping("/listAlgoNodeByType")
    public ResponseMsg<List<TaskNodes>> listAlgoNodeByType(@RequestParam String id) {
        ResponseMsg<List<TaskNodes>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.listAlgoNodeByType(id));

        return responseMsg;
    }

    /**
     * 查询分类树，包含根节点下算法节点
     * @param algoType 0:内置算法、1:自定义算法、2:其他
     * @param showed 0：不显示，1：显示
     * @return
     */
    @ApiOperation(value = "分类树")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "算法分类", name = "algoType", dataType = "String"),
            @ApiImplicitParam(value = "是否显示", name = "showed", dataType = "Int")
    })
    @GetMapping("/getAllNodes")
    public ResponseMsg<List<TaskNodes>> getAllNodes(@RequestParam Integer algoType,
                                                    @RequestParam(required = false) Integer showed) {
        ResponseMsg<List<TaskNodes>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getAllNodes(algoType, showed == null ? 1 : showed));

        return responseMsg;
    }

    /**
     * 生成python文件
     *
     * @return
     */
    @ApiOperation(value = "生成Python文件")
    @GetMapping("/generatePythonFile")
    public ResponseMsg<String> generatePythonFile() {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();

        taskNodesService.generatePythonFile();
        responseMsg.setResponseBody("ok");

        return responseMsg;
    }

    /**
     * 查看项目类型
     * @param projectType 项目类型(PHM中默认为6)
     * @return
     */
    @ApiOperation(value = "查看项目类型")
    @ApiImplicitParams({
       @ApiImplicitParam(value = "工程类型", name = "projectType", dataType = "String"),
       @ApiImplicitParam(value = "是否应用授权", name = "authType", dataType = "Integer")
    })
    @GetMapping("/getProjectTypeList")
    public ResponseMsg<List<ProjectType>> getProjectTypeList(@RequestParam(required = false) String projectType,
                                                             @RequestParam(required = false) Integer authType) {
        ResponseMsg<List<ProjectType>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getProcessTypeList(projectType, authType));

        return responseMsg;
    }

    /**
     * 设置应用范围
     * @param projectTypeId 项目类型ID
     * @param taskNodeIds 算法节点ID（包括分类节点、算法节点）,多个ID使用英文逗号隔开
     * @return
     */
    @ApiOperation(value = "设置应用范围")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "项目类型ID", name = "projectTypeId", dataType = "String"),
            @ApiImplicitParam(value = "算法节点ID,多个ID使用英文逗号隔开", name = "taskNodeIds", dataType = "String"),
            @ApiImplicitParam(value = "算法类型", name = "algoType", dataType = "Integer")
    })
    @PostMapping("/setScope")
    public ResponseMsg<Boolean> setScope(@RequestParam String projectTypeId,
                                         @RequestParam String taskNodeIds,
                                         @RequestParam Integer algoType) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.setScope(projectTypeId, taskNodeIds, algoType));

        return responseMsg;
    }

    /**
     * 查询指定应用下节点列表
     * @param scopeId 应用ID
     */
    @ApiOperation(value = "查询指定应用下节点列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "应用ID", name = "scopeId", dataType = "String"),
            @ApiImplicitParam(value = "算法分类", name = "algoType", dataType = "String")
    })
    @GetMapping("/getTaskNodesByScope")
    public ResponseMsg<List<String>> getTaskNodesByScope(@RequestParam String scopeId, @RequestParam Integer algoType) {
        ResponseMsg<List<String>> responseMsg = new ResponseMsg<>();

        responseMsg.setResponseBody(this.taskNodesService.getTaskNodesByScope(scopeId, algoType));

        return responseMsg;
    }
}
